package com.flowable.modules.rest.api;

import com.flowable.modules.rest.service.FlowableService;
import com.flowable.modules.rest.utils.Result;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/app/rest/process")
public class ProcessApiResource {

    private final static Integer AGREE = 1;
    private final static Integer DISAGREE = 2;

    @Resource
    private FlowableService flowableService;

    /**
     * 根据流程实例id发起流程
     * @param values
     */
    @PostMapping("/launch")
    public Result launch(@RequestBody Map values) {
        try{
            String processDefinitionKey = values.get("processDefinitionKey").toString();
            String businessKey = values.get("businessKey").toString();
            String userId = values.get("userId").toString();

            ProcessInstance start = flowableService.start(processDefinitionKey, businessKey, userId);
            Map result = flowableService.sendNextProcInstId(start.getId(), userId, "发起", AGREE);

            return new Result(Result.SUCCESS, result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.FAILED, e.getCause());
        }
    }

    /**
     * 根据用户信息查询待办列表
     * @param values
     */
    @PostMapping("/getUserTodoList")
    public Result getUserTodoList(@RequestBody Map values) {
        try{
            String userId = values.get("userId").toString();
            List<String> groups = (List<String>) values.get("groups");
            int pageNo = (Integer) values.get("pageNum");
            int pageSize = (Integer) values.get("pageSize");
            List<Map> toDoList = flowableService.getToDoList(userId, groups,pageNo,pageSize);

            return new Result(Result.SUCCESS, toDoList);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(Result.FAILED, e.getCause());
        }
    }

    /**
     * 根据用户信息查询待办任务数
     * @param values
     */
    @PostMapping("/getUserTodoCount")
    public Result getUserTodoCount(@RequestBody Map values) {
        try{
            String userId = values.get("userId").toString();
            List<String> groups = (List<String>) values.get("groups");
            Integer toDoCount = flowableService.getToDoCount(userId, groups);

            return new Result(Result.SUCCESS, toDoCount);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(Result.FAILED, e.getCause());
        }
    }

    /**
     * 根据用户信息查询发起列表
     * @param values
     */
    @PostMapping("/getUserLaunchList")
    public Result getUserLaunchList(@RequestBody Map values) {
        try{
            String userId = values.get("userId").toString();
            int pageNo = (Integer) values.get("pageNum");
            int pageSize = (Integer) values.get("pageSize");
            List<Map> applyList = flowableService.getApplyList(userId,pageSize,pageNo);

            return new Result(Result.SUCCESS, applyList);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(Result.FAILED, e.getCause());
        }
    }

    /**
     * 根据用户信息查询发起任务数
     * @param values
     */
    @PostMapping("/getUserLaunchCount")
    public Result getUserLaunchCount(@RequestBody Map values) {
        try{
            String userId = values.get("userId").toString();
            Integer applyCount = flowableService.getApplyCount(userId);

            return new Result(Result.SUCCESS, applyCount);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(Result.FAILED, e.getCause());
        }
    }

    /**
     * 根据用户信息查询审批任务列表
     * @param values
     */
    @PostMapping("/getUserApprovalList")
    public Result getUserApprovalList(@RequestBody Map values) {
        try{
            String userId = values.get("userId").toString();
            int pageNo = (Integer) values.get("pageNum");
            int pageSize = (Integer) values.get("pageSize");
            List<Map> approvalList = flowableService.getApprovalList(userId,pageSize,pageNo);

            return new Result(Result.SUCCESS, approvalList);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(Result.FAILED, e.getCause());
        }
    }

    /**
     * 用户信息查询审批任务数
     * @param values
     */
    @PostMapping("/getUserApprovalCount")
    public Result getUserApprovalCount(@RequestBody Map values) {
        try{
            String userId = values.get("userId").toString();
            Integer approvalCount = flowableService.getApprovalCount(userId);

            return new Result(Result.SUCCESS, approvalCount);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(Result.FAILED, e.getCause());
        }
    }

    /**
     * 根据用户信息送下一步
     * @param values
     */
    @PostMapping("/sendNext")
    public Result sendNext(@RequestBody Map values) {
        try{
            String userId = values.get("userId").toString();
            String stateFlag = values.get("stateFlag").toString();
            String taskId = values.get("taskId").toString();
            String comment = values.get("comment").toString();
            Map result = flowableService.sendNextTaskId(taskId, userId, comment, Integer.parseInt(stateFlag));
            return new Result(Result.SUCCESS, result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.FAILED, e.getCause());
        }
    }

    /**
     * 根据业务id获取审批详情(包括未办结和已办结流程)
     * @param values
     */
    @PostMapping("/getApprovalDetail")
    public Result getApprovalDetail(@RequestBody Map values) {
        try{
            String businessKey = values.get("businessKey").toString();

            Map approvalDetail = flowableService.getApprovalDetail(businessKey);
            return new Result(Result.SUCCESS, approvalDetail);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.FAILED, e.getCause());
        }
    }

    /**
     * 管理员查看全部流程列表
     */
    @PostMapping("/getTaskList")
    public Result getTaskList(@RequestBody Map values) {
        try{
            int pageNo = (Integer) values.get("pageNum");
            int pageSize = (Integer) values.get("pageSize");
            List<Map> adminTaskList = flowableService.getAdminTaskList(pageSize,pageNo);
            return new Result(Result.SUCCESS, adminTaskList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.FAILED, e.getCause());
        }
    }

    /**
     * 根据业务id和用户身份判断是否允许进行审批操作
     * @param values
     * @return
     */
    @PostMapping("/checkApproveByBusinessKeyAndUserIdentity")
    public Result checkApproveByBusinessKeyAndUserIdentity(@RequestBody Map values) {
        try{
            String businessKey = values.get("businessKey").toString();
            String userId = values.get("userId").toString();
            List<String> groups = (List<String>) values.get("groups");
            boolean result = flowableService.checkApproveByBusinessKeyAndUserIdentity(businessKey, userId, groups);
            return new Result(Result.SUCCESS, result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.FAILED, e.getCause());
        }
    }

}
